/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alggen;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

/**
 *
 * @author Jedrek
 */
public class MyCanvas2 extends Canvas{

    Wyniki wyniki;
    int width, height;
    Color color1, color2;
    public boolean wholePop = false;
    
    public MyCanvas2(Wyniki wyniki){
        this.setBackground(Color.white);
        this.setSize(600,500);
        width = 600;
        height = 500;
        this.wyniki = wyniki;
        color1 = Color.getHSBColor(0.35f, 0.4f, 1f);
        color2 = Color.blue;
    }

    public void paint(Graphics g2){

        Image buffer = this.createImage(this.getWidth(), this.getHeight());
        Graphics g = buffer.getGraphics();

        g.clearRect(0, 0, width, height);

        if(wyniki.validCount>0){
            int yOffset = 30;
            float xScale = (float)width/(float)wyniki.wyniki.length;
            float yScale;
            if(wholePop)
                yScale = (float)(height-yOffset-yOffset)/((float)Math.log(wyniki.worst)-(float)Math.log(wyniki.best));
            else
                yScale = (float)(height-yOffset-yOffset)/((float)Math.log(wyniki.worstFromBest)-(float)Math.log(wyniki.best));

            int lastX = 0;
            int lastYWorst = height-(int)(yScale*((float)Math.log(wyniki.wyniki[0][0])-(float)Math.log(wyniki.best))+yOffset);
            int lastY = height-(int)(yScale*((float)Math.log(wyniki.wyniki[0][1])-(float)Math.log(wyniki.best))+yOffset);
            int lastYBest = height-(int)(yScale*((float)Math.log(wyniki.wyniki[0][2])-(float)Math.log(wyniki.best))+yOffset);

            int x, y;

            int [] xPoints = new int[4];
            int [] yPoints = new int[4];

            xPoints[0] = 0;
            xPoints[3] = 0;

            yPoints[0] = lastYWorst;
            yPoints[3] = lastYBest;

            if(wholePop){
                g.setColor(color1);
                for(int i = 1; i<wyniki.validCount; i++){

                    x = (int)(i*xScale);
                    xPoints[1] = x;
                    xPoints[2] = x;
                        

                    yPoints[1] = height-(int)(yScale*((float)Math.log(wyniki.wyniki[i][0])-(float)Math.log(wyniki.best))+yOffset);
                    yPoints[2] = height-(int)(yScale*((float)Math.log(wyniki.wyniki[i][2])-(float)Math.log(wyniki.best))+yOffset);

                    if(xPoints[1]==xPoints[0]){
                        xPoints[1]++;
                        xPoints[2]++;
                        g.fillPolygon(xPoints, yPoints, 4);
                        xPoints[1]--;
                        xPoints[2]--;
                    }
                    else
                        g.fillPolygon(xPoints, yPoints, 4);

                    xPoints[0] = xPoints[1];
                    xPoints[3] = xPoints[2];

                    yPoints[0] = yPoints[1];
                    yPoints[3] = yPoints[2];
                }
            }

            g.setColor(color2);
            for(int i = 1; i<wyniki.validCount; i++){
                x = (int)(i*xScale);
                y = height-(int)(yScale*((float)Math.log(wyniki.wyniki[i][1])-(float)Math.log(wyniki.best))+yOffset);
                g.drawLine(lastX, lastY, x, y);
                lastX = x;
                lastY = y;
            }

            g.setColor(Color.black);
            g.drawLine(0, 30, 20, 30);
            if(wholePop)
                g.drawString(wyniki.worst+"", 25, 35);
            else
                g.drawString(wyniki.worstFromBest+"", 25, 35);
            g.drawLine(0, height-30, 20, height-30);
            g.drawString(wyniki.best+"", 25, height-25);
        }

        g2.drawImage(buffer, 0, 0, null);
    }

    public void update(Graphics g){
        paint(g);
    }
}
